<template>
	<view class="container">
		<view class="gaps">

		</view>
		<view class="order-pay-state">
			<text v-if="order.payState === 2">待付款</text>
			<text v-else-if="order.payState === 3">支付成功</text>
			<text v-else>订单完成</text>
		</view>
		<view class="gaps">

		</view>
		<view class="sq-panel">
			<view class="panel-tip">
				下单信息
			</view>
			<view class="panel-body">
				<uni-list :border="false">
					<uni-list-item>
						<template v-slot:header>
							<view class="">
								<image :src="imageSrc(order.userAvatar)" class="goods-img"></image>
							</view>
						</template>
						<template v-slot:body>
							<view class="goods-body">

								<view class="goods-intro">
									<view class="goods-name">{{order.userName}}</view>
									<view class="goods-price">
										{{formatDateTime(order.createTime)}}
									</view>
								</view>

							</view>
						</template>
						<template v-slot:footer>
							<view class="goods-sum-price" style="min-width: 60px;">
								{{orderNum(order)}}
							</view>
						</template>

					</uni-list-item>
				</uni-list>
			</view>
		</view>
		<view class="gaps">

		</view>

		<template v-if="order.addressId">
			<template v-if="order.addressId == 0">
				<view class="sq-panel">
					<view class="panel-tip">
						到店自提
					</view>
					<view class="panel-body">
						{{order.shopAddress}}
					</view>
				</view>
			</template>
			<template v-else>
				<view class="sq-panel">
					<view class="panel-tip">
						配送地址
					</view>
					<view class="panel-body">
						<view class="need-has-peisong">
							<view class="address">
								{{order.province + order.city  + order.district + order.address}}
							</view>
							<view class="contact">
								{{order.name}} {{order.phone}}
							</view>
						</view>
					</view>
				</view>

			</template>
		</template>
		<view class="gaps">

		</view>
		<view class="sq-panel">
			<view class="panel-tip">
				订单内容
			</view>
			<view class="panel-body">
				<uni-list>
					<uni-list-item title="订单号" :right-text="order.id">
						<template slot="footer">
							{{order.id}}
						</template>
					</uni-list-item>
					<template v-if="order.deskName">
						<uni-list-item title="桌子号">
							<template slot="footer">
								{{order.deskName}}
							</template>
						</uni-list-item>
					</template>
					<template v-if="order.serialNum">
						<uni-list-item title="流水号"><template slot="footer">
								{{order.serialNum.toString().padStart(4, '0')}}
							</template>
						</uni-list-item>
					</template>
					<uni-list-item v-for="item in order.orderDetailVos" :key="item.id">
						<template v-slot:header>
							<view class="">
								<image :src="imageSrc(item.goodsPictures)" class="goods-img"></image>
							</view>
						</template>
						<template v-slot:body>
							<view class="goods-body">

								<view class="goods-intro">
									<view class="goods-name">{{item.goodsName}}</view>
									<view class="goods-price">
										{{priceTransform(item.buyPrice) + '/' + item.goodsSpec + ' X ' + item.goodsNum}}
									</view>
								</view>

							</view>
						</template>
						<template v-slot:footer>
							<view class="goods-sum-price">
								{{priceTransform(item.totalPrice)}}
							</view>
						</template>
					</uni-list-item>
				</uni-list>
				<view class="order-jine">
					<view>订单金额</view>
					<view>合计 {{priceTransform(order.orderMoney)}}</view>
				</view>
				<view class="line"></view>

				<view class="order-account-info" v-if="order.payState !== 2">
					<view class="order-account order-qianbaokouchu">
						<view>消费折返</view>
						<view class="order-kouchu-text"> -{{priceTransform(order.backMoney)}}</view>
					</view>
					<view class="order-account order-shijizhifu">
						<view>实际收入</view>
						<view class="order-zhifu-text">{{priceTransform(order.incomeMoney)}}</view>
					</view>
				</view>
				<view class="order-opre" v-if="order.payState === 3">
					<button type="primary" class="complate-order-btn" @click="completeOrderClk">完成</button>
				</view>
			</view>
		</view>
		<view class="gaps"></view>


		
	</view>
</template>

<script>
	import {
		getOrderDetail,
		confirmOrder,
		completeOrder
	} from '@/api/shops/preOrderDetail/preOrderDetail.js'
	import {
		transform
	} from '@/minix/mixin.js'
	export default {
		mixins: [transform],
		data() {
			return {
				orderId: "",
				order: {},
				shopName: '',
			}
		},
		onLoad: function(option) {

			this.orderId = option.id
			this.init()
		},
		methods: {
			init() {
				let _this = this
				getOrderDetail(this.orderId).then((res) => {
					this.order = res
					this.shopName = res.shopName
				})
			},
			orderNum(item) {
				if (item.serialNum) {
					return '流水号:' + item.serialNum.toString().padStart(4, '0')
				}
				if (item.deskName) {
					return '桌号:' + item.deskName
				}
				return '';
			},
			completeOrderClk() {
				uni.showModal({
					title: '确定完成',
					success: (res) => {
						if (res.confirm) {
							completeOrder({
									orderId: this.orderId
								})
								.then((res) => {
									if (res > 0) {
										this.order.payState = 1
										uni.showModal({
											title: '订单完成',
											showCancel: false,
										})
									}
								})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.order-pay-state {
		background-color: #fff;
		height: 40px;
		color: red;
		text-align: center;
		line-height: 40px;
	}


	.orderin-info {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		background-color: #fff;
		margin: 10px 0;
		padding: 10px 0;

		.order-person {
			display: flex;
			flex-direction: column;
			align-items: flex-start;

			.person-content {
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
			}
		}

		.user-image {
			width: 50px;
			height: 50px;
		}
	}



	.order-status {
		background-color: #e8f2e1;
		height: 50px;
		line-height: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #94c293;
		font-weight: bold;
	}

	.order-detail-header {
		display: flex;
		flex-direction: row;
		background-color: #fff;
		padding: 10px;


		image {
			width: 100rpx;
			height: 100rpx;
		}

		.order-shop {
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			flex: 1;
			margin-left: 10px;

			.order-shop-name {
				font-size: 14px;
				font-weight: bold;
			}

			.order-shop-open-time {
				font-size: 12px;
				color: #cfc9c5;
			}
		}

		.order-shop-disc {
			display: flex;
			flex-direction: row;
			justify-content: flex-end;
			border-radius: 5px;
			padding: 5px;
			background-color: #fff1db;
			color: #ffac46;
			font-weight: bold;
			line-height: 1;
		}
	}

	.order-price-detail {
		.order-header {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding: 10px;
			background-color: #fafcff;

			.header-tip {}

			.header-status {
				color: red;
				font-weight: bold;
			}
		}


		.order-price-title {
			font-size: 13px;
			font-weight: bold;
			margin: 10px 0;
			margin-left: 10px;

		}

		.order-price-content {
			display: flex;
			flex-direction: column;
			background-color: #fff;

			.order-price-content-item {
				display: flex;
				flex-direction: row;

				padding: 10px;

				image {
					width: 100rpx;
					height: 100rpx;
				}

				.order-price-intro {
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					flex: 1;
					margin-left: 10px;

					.order-price-name {
						font-size: 14px;
						font-weight: bold;
					}

					.order-price-guige {
						font-size: 12px;
						color: #cfc9c5;
					}
				}

				.order-price-price {
					font-size: 14px;
					font-weight: bold;
					display: flex;
					flex-direction: row;
					justify-content: flex-end;
				}
			}

			.order-jine {
				display: flex;
				flex-direction: row;
				justify-content: space-between;
				padding: 0 10px;
				font-weight: 600;
			}

			.line {
				border-bottom: 1px solid #cfc9c5;
				margin: 10px 0;
			}

			.order-account-info {
				display: flex;
				flex-direction: column;
				font-weight: bold;
				border-bottom: 1px solid #ababab;

				.order-account {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					padding: 0 10px;
					margin: 5px 0;
				}

				.order-kouchu-text {
					color: red;
				}

				.order-zhifu-text {
					font-size: 17px;
				}

				.order-zhefan-text {
					font-size: 13px;
					color: #7bf30f;
				}

			}

			.order-opre {
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				margin: 10px;

				.order-opre-btn {
					background-color: #7bf30f;
					color: #fff;
				}
			}
		}
	}

	.order-jine {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding: 0 10px;
		font-weight: 600;
		margin-top: 10px;
	}

	.line {
		border-bottom: 1px solid #cfc9c5;
		margin: 5px 0;
	}

	.order-account-info {
		display: flex;
		flex-direction: column;
		font-weight: bold;

		.order-account {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			padding: 0 10px;
			margin: 5px 0;
		}

		.order-kouchu-text {
			color: red;
		}

		.order-zhifu-text {
			font-size: 17px;
		}

		.order-zhefan-text {
			font-size: 13px;
			color: #7bf30f;
		}

	}

	.order-opre {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		margin: 10px;
		padding-bottom: 10px;

		.order-opre-btn {
			background-color: #7bf30f;
			color: #fff;
		}
	}


	.order-price-info {
		margin-top: 20px;
		background-color: #fff;
		padding: 10px;

		.order-price-info-title {
			font-weight: bold;
			margin-bottom: 10px;

		}

		.order-meta {
			font-size: 13px;
			color: #cfc9c5;
		}

	}

	.order-address {
		margin-left: 10px;
		font-size: 16px;
	}


	.complate-order-btn {
		width: 100%;
	}


	.goods-img {
		width: 60px;
		height: 60px;
		border-radius: 5px;
	}

	.goods-body {
		margin-left: 8px;
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.goods-intro {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.goods-name {
		font-size: 16px;
		font-weight: bold;
	}

	.goods-price {
		font-size: 14px;
	}

	.goods-sum-price {
		display: flex;
		align-items: center;
	}
</style>